import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/index'
  },
  {
    path: '/index',
    name: 'Index',
    component: () => import(/* webpackChunkName: "index" */ '../views/Index.vue'),
    children: [
      {
        path: 'testcase',
        name: 'Testcase',
        component: () => import(/* webpackChunkName: "index" */ '../views/Testcase.vue'),
      },
      {
        path: 'plan',
        name: 'Plan',
        component: () => import(/* webpackChunkName: "index" */ '../views/Plan.vue'),
      },
      {
        path: 'record',
        name: 'Record',
        component: () => import(/* webpackChunkName: "index" */ '../views/Record.vue'),
      }
    ]
  },
  {
    path: '/user',
    name: 'User',
    component: () => import(/* webpackChunkName: "index" */ '../views/User.vue'),
  },
]

const router = new VueRouter({
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 判断是否要去登录页面
  if (to.path == '/user') {
    // 放行
    next()
  } else {
    // 去其他路由页面，判断是否有登录的token
    const token = localStorage.getItem('token')
    // 如果没有token(未登录)
    if (!token) {
      // 强制进入登录页面
      next('/user')
    } else {
      // 放行
      next()
    }
  }
})

export default router
